package com.zto.sch.advance.excel;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2021-07-29 18:35
 **/
@RestController
@RequestMapping("excel/easyexcel")
@Slf4j
public class EazyExcelController {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private ExcelService excelService;

    /**
     * 文件上传
     * <p>1. 创建excel对应的实体对象 参照{@link UploadData}
     * <p>2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link UploadDataListener}
     * <p>3. 直接读即可
     */
    @PostMapping("upload")
    @ResponseBody
    public String upload(@RequestParam(value = "excelFile") MultipartFile excelFile) throws IOException, InterruptedException {
//        EasyExcel.read(file.getInputStream(), UploadData.class, new UploadDataListener(uploadDAO)).sheet().doRead();
        try {
            AsyncTaskExecutor asyncTaskExecutor = (AsyncTaskExecutor) applicationContext.getBean("memberExecutor");
            asyncTaskExecutor.execute(() -> {
                Thread.currentThread().setName("导入客户:" + Thread.currentThread().getName());
                excelService.readFileEasyExcel(excelFile.getOriginalFilename());
            });

        } catch (Exception e) {
            log.error("报错了....", e);
            return "fail";
        }
        return "success";
    }

    @PostMapping("write")
    @ResponseBody
    public String write(@RequestParam("count") Integer count) throws IOException, InterruptedException {
        try {
            AsyncTaskExecutor asyncTaskExecutor = (AsyncTaskExecutor) applicationContext.getBean("memberExecutor");
            asyncTaskExecutor.execute(() -> {
                Thread.currentThread().setName("导入客户:" + Thread.currentThread().getName());
//                excelService.readFile(excelFile.getOriginalFilename());
                excelService.writeFileEasyExcel(count);
            });


        } catch (Exception e) {
            log.error("报错了....", e);
            return "fail";
        }
        return "success";
    }

    @PostMapping("test")
    @ResponseBody
    public String test() throws IOException, InterruptedException {
        try {
            AsyncTaskExecutor asyncTaskExecutor = (AsyncTaskExecutor) applicationContext.getBean("memberExecutor");
            asyncTaskExecutor.execute(() -> {
                Thread.currentThread().setName("导入客户:" + Thread.currentThread().getName());
                System.out.println(Thread.currentThread().getName());
            });


        } catch (Exception e) {
            log.error("报错了....", e);
            return "fail";
        }
        return "success";
    }

    @PostMapping("test2")
    @ResponseBody
    public String test2() throws IOException, InterruptedException {
        try {
            AsyncTaskExecutor asyncTaskExecutor = (AsyncTaskExecutor) applicationContext.getBean("memberExecutor");
            for (int i = 0; i < 30; i++) {
                asyncTaskExecutor.execute(() -> {
                    Thread.currentThread().setName("导入客户:" + Thread.currentThread().getName());
                    System.out.println(Thread.currentThread().getName());
                });
            }


        } catch (Exception e) {
            log.error("报错了....", e);
            return "fail";
        }
        return "success";
    }

    @PostMapping("test3")
    @ResponseBody
    public String test3() throws IOException, InterruptedException {
        try {
            AsyncTaskExecutor asyncTaskExecutor = (AsyncTaskExecutor) applicationContext.getBean("memberExecutor");

            for (int i = 0; i < 50; i++) {
                CompletableFuture.supplyAsync(() -> importCustomers(), asyncTaskExecutor);

            }
        } catch (Exception e) {
            log.error("报错了....", e);
            return "fail";
        }
        return "success";
    }

    private String importCustomers() {
        for (int i = 0; i < 10; i++) {
            System.out.println("当前线程：" + Thread.currentThread().getName());
        }

        return "ok";
    }
}
